# -*- coding: UTF-8 -*-
# Project : bio_tc
# File : times.py
# IDE : PyCharm
# Author : 博科（鑫贝西）田聪
# Date : 2021/11/25 14:06
# 时间处理

import re
import time

import cn2an

from datetime import datetime
from dateutil.relativedelta import relativedelta


class Time:

    @staticmethod
    def str_to_stamp(string: str):
        # 字符串转时间戳
        if '前' in string:
            return Time.timeq(string)
        if "年" in string or '月' in string or '日' in string:
            return Time.timez(string)
        if '-' in string or '/' in string or '.' in string:
            return Time.timef(string)


    @staticmethod
    def stamp_to_str(string: str):
        # 时间戳转字符串
        # 秒级和毫秒级

        # 秒级
        if len(string) == 10:
            time_local = time.localtime(int(string))
            return time.strftime('%Y-%m-%d %H:%M', time_local)
        if len(string) == 13:
            if type(string) == 'int':
                time_local = time.localtime(int(str(string)[:-3]))
            else:
                time_local = time.localtime(int(string[:-3]))
            return time.strftime('%Y-%m-%d %H:%M', time_local)

    @staticmethod
    def timec():
        pass

    @staticmethod
    def howlong(string: str):
        # 距今多久
        nowdata = datetime.now()
        old = datetime.fromtimestamp(Time.str_to_stamp(string))
        return (nowdata-old).days

    @staticmethod
    def timez(string: str):
        # 中文的时间转时间戳
        # re_1 = '(\d{2,4})年(\d{1,2})月\d{1,2}日'
        if ':' in string:
            return int(datetime.strptime(string, '%Y年%m月%d日 %H:%M').timestamp())
        return int(datetime.strptime(string, '%Y年%m月%d日').timestamp())

    @staticmethod
    def timef(string: str):
        # 标准的时间转时间戳

        if '-' in string:
            if ':' in string:
                return int(datetime.strptime(string, '%Y-%m-%d %H:%M').timestamp())
            return int(datetime.strptime(string, '%Y-%m-%d').timestamp())
        if '/' in string:
            if ':' in string:
                return int(datetime.strptime(string, '%Y/%m/%d %H:%M').timestamp())
            return int(datetime.strptime(string, '%Y/%m/%d').timestamp())
        if '.' in string:
            if ':' in string:
                return int(datetime.strptime(string, '%Y.%m.%d %H:%M').timestamp())
            return int(datetime.strptime(string, '%Y.%m.%d').timestamp())

    @staticmethod
    def timeq(string: str):
        # 几个小时前，几天前
        string = re.sub('个','',string)
        string = re.sub('两','二',string)
        if '分钟' in string:
            try:
                minu = re.findall('(\d+)分钟前', string)[0]
            except:
                minu = cn2an.cn2an(re.findall('([\u4e00-\u9fff]+)分钟前', string)[0])
            pubdate = int((datetime.now() - relativedelta(minutes=int(minu))).timestamp())

        elif '小时' in string:
            try:
                minu = re.findall('(\d+)小时前', string)[0]
            except:
                minu = cn2an.cn2an(re.findall('([\u4e00-\u9fff]+)小时前', string)[0])
            pubdate = int((datetime.now() - relativedelta(hours=int(minu))).timestamp())

        elif '天' in string:
            try:
                minu = re.findall('(\d+)天前', string)[0]
            except:
                minu = cn2an.cn2an(re.findall('([\u4e00-\u9fff]+)天前', string)[0])

            pubdate = int((datetime.now() - relativedelta(days=int(minu))).timestamp())

        elif '月' in string:

            try:
                minu = re.findall('(\d+)月前', string)[0]
            except:
                minu = cn2an.cn2an(re.findall('([\u4e00-\u9fff]+)月前', string)[0])

            pubdate = int((datetime.now() - relativedelta(months = int(minu))).timestamp())

        elif '年' in string:

            try:
                minu = re.findall('(\d+)年前', string)[0]
            except:
                minu = cn2an.cn2an(re.findall('([\u4e00-\u9fff]+)年前', string)[0])

            pubdate = int((datetime.now() - relativedelta(years = int(minu))).timestamp())

        return pubdate


if __name__ == '__main__':
    print(Time.howlong('一年前'))
